

//
//  HMTextView.m
//  黑马微博-wzy
//
//  Created by Mac on 2017/2/13.
//  Copyright © 2017年 Mac. All rights reserved.
//

#import "HMTextView.h"

@interface HMTextView()

@property (nonatomic, strong) UILabel *placehoderLabel;

@end


@implementation HMTextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 初始化控件,该方法只会调用一次
        self.backgroundColor = [UIColor clearColor];
        //设置 placehoderLabel
        [self setupPlacehoderLabel];

        
    }
    return self;
}

//设置placehoderLabel
- (void)setupPlacehoderLabel{

    //1.显示占位文字的label
    UILabel *placehoderLabel = [[UILabel alloc] init];
    placehoderLabel.numberOfLines = 0;
    placehoderLabel.backgroundColor = [UIColor clearColor];
    [self addSubview:placehoderLabel];
    
    self.placehoderLabel = placehoderLabel;
    
    //2.设置占位文字的默认颜色
    self.placehoderColor = [UIColor lightGrayColor];
    //3.设置默认字体
    self.font = [UIFont systemFontOfSize:14];
    //4.监听内部文字改变
    /*
     [[NSNotificationCenter defaultCenter] addObserver:B selector:@selector(方法) name: 通知名称  object:A]
     监听A发出的通知:只要A发出名称为”通知名称”的通知,B就会调用”方法”
     */
    //只有用户通过键盘改变文字的内容,才会发通知
    //如果在代码中中途改变(例如点击cancel 按钮的时候 更改文字内容,是不会发通知的,严禁起见,重写// - (void)setText:(NSString *)text{ } 方法// - (void)setAttributedText:(NSAttributedString *)attributedText)
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:self];
}

- (void)dealloc{
    //移除监听
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 监听文字改变
- (void)textDidChange{
    //text:只包含普通的文本字符串
    //attributeText:包含了显示在textView里面的所有内容(表情/text)
    self.placehoderLabel.hidden = self.hasText;
}

#pragma mark - 公共方法
- (void)setText:(NSString *)text{
    [super setText:text];
    
    [self textDidChange];
}

- (void)setAttributedText:(NSAttributedString *)attributedText{
    [super setAttributedText:attributedText];
    
    [self textDidChange];
}

#pragma mark - 重写placehoder
- (void)setPlacehoder:(NSString *)placehoder{
    //如果前面声明的时候是copy,最好这么写
    _placehoder = [placehoder copy];
    //设置文字
    self.placehoderLabel.text = placehoder;
    
    //可能中途改placehoder内容多少,需重新计算子控件的frame
    [self setNeedsLayout];
}

#pragma mark - placehoderColor
- (void)setPlacehoderColor:(UIColor *)placehoderColor{
    _placehoderColor = placehoderColor;

    //1.设置颜色
    self.placehoderLabel.textColor = placehoderColor;
}

#pragma mark - 重写 font
- (void)setFont:(UIFont *)font{
    //1.重写父类的方法
    [super setFont:font];
    //2.占位图的文字大写始终等于textView 的文字
    self.placehoderLabel.font = font;
    //3.可能中途改placehoder 的font大小,需重新计算子控件的frame
    [self setNeedsLayout];
}

// 布局
- (void)layoutSubviews{
    [super layoutSubviews];
    
    //1.设置placehoderLabel 的frame
    self.placehoderLabel.x = 5;
    self.placehoderLabel.y = 8;
    self.placehoderLabel.width = kScreenWidth - 2 *self.placehoderLabel.x;
    
    //2.根据文字计算label 的高度
    CGFloat placehoderHeight = [self.placehoderLabel sizeThatFits:CGSizeMake(self.placehoderLabel.width, 0)].height;
    self.placehoderLabel.height = placehoderHeight;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
